
var page_no = 0;
// 监听页面滚动的高度，确定抢购时间是否选择固定
$(window).scroll(function() {
    var h = $(document).height() - $(document).scrollTop() - $(window).height();
    var status = $('.ajax-tips p').attr('status');
    if (h < 1200 && status === 'on') {
        $('.ajax-tips p').attr('status', 'off');
        page_no++;
        console.log("page==="+page_no);
        $.ajax({
            url : '/lqm/item',
            type : 'POST',
            data : {
                pageno : page_no,
                rulesId : rulesId,
                sortId : sortId
            },
            dataType : 'json',
            success : function(result) {
                if (result.code != 0) {
                    $('.ajax-tips p').text('使出吃奶的力气也没有找到更多的宝贝了~~~');
                    $('.ajax-tips p').attr('status', 'off');
                } else {
                    addItems(result);
                    $('.ajax-tips p').attr('status', 'on');
                }
            }
        });
    }
});

// 将ajax获取到的数据插入到列表中
function addItems(data) {
    var itemhtml = '';
    for(i = 0; i < data.data.length; i++) {
        var item = data.data[i];
        var price_old,price_now,tzurl,price_quan;
        if (item.title != null) {
            tzurl = item.couponShareUrl;  //跳转地址
            price_old = item.zkFinalPrice;//原价
            price_quan = item.couponAmount;//优惠价
            price_now = price_old-price_quan;

            price_now = Math.round(parseFloat(price_now)*100)/100;
            if(price_now.toString().indexOf('.') < 0){
                price_now = price_now.toString()+'.00'
            }
            if(price_now.toString().indexOf('.') == price_now.toString().length-2){
                price_now = price_now.toString()+'0'
            }
            if (item.user_type == 0) {
                img_name = 'tmall32';
            } else {
                img_name = 'taobao32';
            }
            itemhtml += '<div class="col-xs-3 item-box"><a href="'+tzurl+'" title="'+item.title+'" target="_blank"><div class="item">'
            itemhtml +=       '<div class="img-box"><img class="lazy" src="'+item.pictUrl+'"></div>'
            itemhtml +=       '<h2><img src="/pcstyle/images/'+img_name+'.png" alt="淘宝优惠券logo"> '+item.title+'</h2><hr>'
            itemhtml +=       '<div class="info">'
            itemhtml +=         '<div class="row price-now"><div class="col-xs-12"><div class="pull-left">现价￥'+price_old+'</div><div class="pull-right">月销:'+item.volume+'</div></div></div>'
            itemhtml +=         '<div class="row price-ori"><div class="col-xs-12">'
            itemhtml +=             '<div class="pull-left">券后<span class="prefix">￥</span><span class="number">'+price_now+'</span></div>'
            itemhtml +=             '<div class="pull-right"><div class="circle-left"></div><div class="circle-right"></div>￥<span class="money">'+price_quan+'</span>元券</div></div>'
            itemhtml +=  '</div></div></div></a></div>'
        }
    }
    $('.coupon-list .container>.row').append(itemhtml);

}